<template>
  <view>
    <view class="sticky-box">
      <MySearch @click="goSearch" bgcolor="teal" radius="30">
        哈哈哈哈
      </MySearch>
    </view>

    <!-- 
      标签是小程序的! 但是语法是vue的!!!
     -->
    <!-- 轮播图区域 -->
    <swiper
      class="top-swiper"
      indicator-dots
      autoplay
      :interval="3000"
      :duration="1000"
      circular
    >
      <!-- 循环渲染轮播图的 item 项 -->
      <swiper-item
        v-for="(item, index) in swiperList"
        :key="index"
        @click="goProductDetail(item)"
      >
        <view class="swiper-content">
          <image :src="item.image_src"></image>
        </view>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="goNextPage(item)"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>

    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>

        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="goToProductList(item.product_list[0])"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view class="right-img-item" v-if="idx !== 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  @click="goToProductList(product)"
                  mode="widthFix"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import MySearch from '@/components/my-search.vue'

export default {
  data() {
    return {
      // 1. 准备数据
      swiperList: [],
      // 1. 分类导航的数据列表
      navList: [],

      // 1. 楼层的数据列表
      floorList: [],
    }
  },

  components: {
    MySearch,
  },

  created() {
    // 2. 调用函数
    this.getSwiperList()
    this.getNavList()
    this.getFloorList()
  },

  methods: {
    goSearch() {
      // console.log('跳转了...')
      uni.navigateTo({
        url: '/pages_sub1/search/search',
      })
    },

    // 3. 封装函数请求接口数据
    async getSwiperList() {
      const { message, meta } = await this.$http.get('/home/swiperdata')

      if (meta.status !== 200) return this.$msg()

      this.swiperList = message
    },

    goProductDetail(item) {
      // console.log(item)  跨端
      uni.navigateTo({
        url: '/pages_sub1/product-detail/product-detail?pid=' + item.goods_id,
      })
    },

    // 3. 获取分类导航数据的方法
    async getNavList() {
      const { meta, message } = await this.$http.get('/home/catitems')

      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }

      this.navList = message
    },

    goNextPage(item) {
      // console.log(item)
      if (item.name === '分类') {
        uni.switchTab({
          url: '/pages/category/category',
        })
      }
    },

    // 3. 获取楼层数据的方法
    async getFloorList() {
      const { meta, message } = await this.$http.get('/home/floordata')

      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }

      this.floorList = message
    },

    goToProductList(product) {
      // console.log(product.navigator_url)  参数信息
      const temp = product.navigator_url.split('?')[1] // query=户外

      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?' + temp,
      })
    },
  },
}
</script>

<style lang="scss">
.sticky-box {
  // 粘性定位
  position: sticky;

  // 定位条件
  top: 0;

  // 提升层级
  z-index: 999999;
}

.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
